﻿using System;

using LeanEngine.Configuration;

namespace LeanEngine.Data.Linq
{
    internal sealed class DbFunction : IFunction
    {
        #region Singleton Members

        private static DbFunction _instance = new DbFunction();

        public static DbFunction Instance { get { return _instance; } }

        private DbFunction()
        {
            var defaultConnection = AppSetting.GetConnection("defaultConnection");
            this.Function = (defaultConnection.ProviderName == "System.Data.SqlClient")
                ? new SqlClient.SqlFunction()
                : PluginProvider.LoadInterface<IFunction>();
        }

        #endregion

        #region PropertyInfo Members

        private IFunction Function { get; set; }

        #endregion

        #region IFunction Members

        public string FormatValue(object value)
        {
            return this.Function.FormatValue(value);
        }

        public string FormatValue(object value, string format)
        {
            return this.Function.FormatValue(value, format);
        }

        public string WrapInBrackets(string value, string tableAlias)
        {
            return this.Function.WrapInBrackets(value, tableAlias);
        }

        #endregion

        #region Method Members

        internal string GetBoolValue(bool value)
        {
            return value ? "1" : "0";
        }

        #endregion
    }
}